package br.unifor.mia.olio.web;

import java.util.List;
import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import br.unifor.mia.olio.SessionRemote;

@Path("Execution")
public class Execution {

	protected Logger logger = Logger.getLogger(getClass().getName());

	protected SessionRemote remote = null;

	/**
	 * Default constructor.
	 */
	public Execution() {
		// TODO Auto-generated constructor stub
	}

	public SessionRemote getFacade() {

		if (remote == null) {

			try {
				InitialContext ic = new InitialContext();

				remote = (SessionRemote) ic.lookup("java:global/OlioTestAnaliser/OlioTestAnaliserEJB/Session");
			} catch (NamingException e) {
				logger.severe(e.toString());
			}
		}

		return remote;
	}

	/**
	 * Retrieves representation of an instance of Execution
	 * 
	 * @return an instance of String
	 */
	@GET
	@Produces("text/plain")
	public List<br.unifor.mia.olio.Execution> findAllExecution() {
		return getFacade().findAllExecution();
	}

	/**
	 * PUT method for updating or creating an instance of Execution
	 * 
	 * @param content
	 *            representation for the resource
	 * @return an HTTP response with content of the updated or created resource.
	 */
	@POST
	@Consumes("application/json")
	@Produces("application/json")
	public br.unifor.mia.olio.Execution createExecution(br.unifor.mia.olio.Execution execution) {
		System.out.println("web execution ["+execution+"]");
		
		return getFacade().createExecution(execution);
	}
	
	@GET
	@Path("/id/{id}")
	@Produces("application/json")
	public br.unifor.mia.olio.Execution loadExecution(@PathParam("id") Long id) {
		
		return getFacade().loadExecution(id);
		
	}

	@GET
	@Path("/ip/{ip}")
	@Produces("text/plain")
	public String loadExecution(@PathParam("ip") String ip) {
		
		getFacade().loadExecutions(ip);
		
		return ip;
		
	}
}